package com.group9.controller;

import com.alibaba.fastjson.JSONArray;
import com.group9.dto.Message;
import com.group9.pojo.CommentMessage;
import com.group9.service.CommentMessageServer;
import com.group9.service.CommentMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;

/**
 * Created by Lcouncillor on 2020/6/11.
 */
@Controller
@RequestMapping(value = "/message")
public class CommentMessageController {

    @Autowired
    CommentMessageService commentMessageService;

    @Autowired
    CommentMessageServer commentMessageServer;

    @RequestMapping(value = "/readed")
    @ResponseBody
    public String changeCommentMessageStatus(String commentId){
        //String userId= (String) session.getAttribute("customerId");
        String userId =  "1001";
        int result = commentMessageService.saveCommentMessageStatus(commentId,userId);

        if (result>0){
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping(value = "/test")
    @ResponseBody
    public void messageTest(){
        Message message = new Message();
        message.setUserId("1002");
        message.setMessage("test");

        Object obj = JSONArray.toJSON(message);
        String json = obj.toString();

        commentMessageServer.onMessage("1002", json);

    }
}
